package sangpil.pv3d
{
	import flash.display.BitmapData;
	import flash.display.IBitmapDrawable;
	import flash.events.Event;
	
	import gs.TweenLite;
	import gs.easing.Back;
	
	import org.papervision3d.materials.BitmapMaterial;
	import org.papervision3d.objects.primitives.Plane;
	import org.papervision3d.view.BasicView;
	
	import sangpil.pv3d.events.RotatingEvent;
	import sangpil.utils.bitmap.GetBitmap;

	[Event(name='complete',type='sangpil.pv3d.events.RotatingEvent')]
	public class RotatingEffect extends BasicView
	{
		private var startMatArr:Array = new Array();
		private var endMatArr:Array = new Array();
		
		private var startPlaneArr:Array = new Array();
		private var endPlaneArr:Array = new Array();
		
		private var mcStart:IBitmapDrawable;
		private var mcEnd:IBitmapDrawable;
		private var arrow:String;
		
		private var planeWidth:Number;
		private var planeHeight:Number;
		
		private var segWidht:Number;
		private var segHeight:Number;
		
		private var segWi:uint;
		private var segHi:uint;
		
		public function RotatingEffect(mcStart:IBitmapDrawable, mcEnd:IBitmapDrawable, width:Number, height:Number, arrow:String = 'right', segWi:uint = 2, segHi:uint = 2)
		{
			trace('rotation effect run');
			this.mcStart = mcStart;
			this.mcEnd = mcEnd;
			this.planeWidth = width;
			this.planeHeight = height;
			this.arrow = arrow;
			this.segWi = segWi;
			this.segHi = segHi;
			
			this.setPapervision3D();
			this.setPlanes();
			
			this.startRendering();
			this.addTween(this.arrow);
		}
		
		private function setPapervision3D():void
		{
			this.camera.zoom = 115.5;
		}
		
		private function setPlanes():void
		{
			this.segWidht = this.planeWidth / this.segWi;
			this.segHeight = this.planeHeight / this.segHi;
			makeMaterials(mcStart, startMatArr);
			makeMaterials(mcEnd, endMatArr);
			
			makePlanes(startMatArr, startPlaneArr, false);
			makePlanes(endMatArr, endPlaneArr, true);
		}
		
		private function makeMaterials(mc:IBitmapDrawable, arr:Array):void
		{	
			var src:BitmapData = GetBitmap.getBitmap(mc, this.planeWidth, this.planeHeight);
			
/* 			var bitmap:Bitmap = new Bitmap(src);
			this.addChild(bitmap); */
 			for(var i:uint = 0; i < this.segWi; i++)
			{
				for(var j:uint = 0; j < this.segHi; j++)
				{
  					arr.push(
					{
 					bm:new BitmapMaterial(GetBitmap.getBitmapSection(src, i*this.segWidht, j*this.segHeight, this.segWidht, this.segHeight))
					,xpos:i*this.segWidht, ypos:j*this.segHeight 
					}
					);  
				}
			} 
		}
		
		
		private function makePlanes(matArr:Array, planeArr:Array, backSide:Boolean):void
		{
			for(var i:uint = 0; i < matArr.length; i++)
			{
				//set materials
				matArr[i].bm.precise = true;
				matArr[i].bm.doubleSided = false;
				matArr[i].bm.smooth = true;
				
				planeArr.push(new Plane(matArr[i].bm, this.segWidht, this.segHeight));
				planeArr[i].x = matArr[i].xpos-this.planeWidth/2+this.segWidht/2;
				planeArr[i].y = (matArr[i].ypos*-1)+this.planeHeight/2-this.segHeight/2;
				if(backSide)
				{
					planeArr[i].rotationY = 180;
				}
				this.scene.addChild(planeArr[i]);
			}
		}
		
		private function addTween(str:String):void
		{
			var angle:Number = 0;
			switch(str)
			{
				case 'right':
				trace('right tween');
				angle = 180;
				break;
				case 'left':
				trace('left tween');
				angle = -180;
				break;
			}
			
			for(var i:uint = 0; i < this.startMatArr.length; i++)
			{
				TweenLite.to(this.startPlaneArr[i], 1, {rotationY:this.startPlaneArr[i].rotationY+angle, ease:Back.easeInOut, onComplete:onFinishTween, onCompleteParams:[1]});
				TweenLite.to(this.endPlaneArr[i], 1, {rotationY:this.endPlaneArr[i].rotationY+angle, ease:Back.easeInOut, onComplete:onFinishTween, onCompleteParams:[1]});
			} 	
		}
		
		private var count:uint = 0;
		private function onFinishTween(num:uint):void
		{
			this.count+= 1;
			if(this.count == this.startPlaneArr.length*2)
			{
				trace('move all');
				this.stopRendering();
				this.dispatchEvent(new RotatingEvent(RotatingEvent.COMPLETE));
			}
		}
		
		override protected function onRenderTick(event:Event=null):void
		{	
			super.onRenderTick(event);
/* 			for(var i:uint = 0; i < this.startMatArr.length; i++)
			{
				//this.startPlaneArr[i].rotationX += 1;
				this.startPlaneArr[i].rotationY += 1;
				this.endPlaneArr[i].rotationY += 1;
			} */ 
		}
		
	}
}